//
//  MQStatusRetweetedView.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/21.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit



class MQStatusRetweetedView: UIView {
    
    private var bottomConstraint:Constraint?
     override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var statusViewModel:MQStatusViewModel?{
        didSet{
            contentLabel.text = statusViewModel?.retweetStatusText
            
            bottomConstraint?.uninstall()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count>0 {
                
                
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                  bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(MQStatusCellMargin).constraint
                    
                    pictureView.picUrls = picUrls
                })
            }else{
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                   bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(MQStatusCellMargin).constraint
                })
            }
        }
    }
    
    func setupUI(){
        backgroundColor = UIColor(white:0.95, alpha: 1)
        
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self).offset(MQStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -MQStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(MQStatusCellMargin)
            make.leading.equalTo(contentLabel)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(MQStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    private lazy var contentLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView:MQStatusPictureView = {
        let pictureView = MQStatusPictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
}



